package com.wzdigit.wms.raw.controller.gdn;

import com.wzdigit.framework.Shift;
import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.framework.model.response.ErrorEntity;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.raw.client.response.gdn.delivery.DeliveryDtlResponse;
import com.wzdigit.wms.raw.client.response.gdn.delivery.DeliveryGoodsResponse;
import com.wzdigit.wms.raw.client.response.gdn.delivery.DeliveryMainResponse;
import com.wzdigit.wms.raw.constant.RawWareConstant;
import com.wzdigit.wms.raw.service.gdn.DeliveryNewService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

/**
 * Created by zyf on 2021/7/8 15:24
 */
@RestController
@Api(tags = "交接确认PDA【新】")
@RequestMapping(value = RawWareConstant.API_VERSION)
public class DeliveryNewController {
    public static final String REQUEST_MODEL = "/deliveryNew";


    @Autowired
    private DeliveryNewService deliveryNewService;

/*    //查询交接确认主页面
    @ApiOperation(value = "获取交接确认主页面【点击返回时再调一遍该接口】", notes = "获取交接确认主页面【点击返回时再调一遍该接口】")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @RequestMapping(value = REQUEST_MODEL + "/SearchMainNew", method = RequestMethod.GET)
    public DeliveryMainResponse searchDeliveryMainNew(
            String gdnNo,
            HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
        try {
            return deliveryNewService.searchMainPage(gdnNo);
        } catch (Exception ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }

    //扫描SN
    @ApiOperation(value = "主页面扫描SN", notes = "主页面扫描SN")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @RequestMapping(value = REQUEST_MODEL + "/ScanSnNew", method = RequestMethod.GET)
    public List<DeliveryDtlResponse> deliveryScanNew(
            String gdnNo, BigDecimal gdnHeaderId, String goodsSn,
            HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
        try {
            return deliveryNewService.scanSnDelivery(gdnNo,gdnHeaderId,goodsSn);
        } catch (Exception ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }

    //点击SN明细跳转
    @ApiOperation(value = "点击SN明细跳转", notes = "点击SN明细跳转")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @RequestMapping(value = REQUEST_MODEL + "/goodsListNew", method = RequestMethod.GET)
    public List<DeliveryGoodsResponse> searchGoodsList(
            String gdnNo, BigDecimal gdnHeaderId,BigDecimal skuId,
            HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
        try {
            return deliveryNewService.searchDeliveryGoods(gdnNo,gdnHeaderId,skuId);
        } catch (Exception ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }

    //SN反扫描
    @ApiOperation(value = "SN反扫描", notes = "SN反扫描")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @RequestMapping(value = REQUEST_MODEL + "/cancelSnNew/{gdnNo}/{gdnHeaderId}/{skuId}", method = RequestMethod.POST)
    public List<DeliveryGoodsResponse> cancelSnNew(
            @PathVariable("gdnNo") String gdnNo,@PathVariable("gdnHeaderId") BigDecimal gdnHeaderId,
            @PathVariable("skuId") BigDecimal skuId,@RequestBody List<String> cancelSnList,
            HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
        try {
            return deliveryNewService.cancelSn(gdnNo,gdnHeaderId,skuId,cancelSnList);
        } catch (Exception ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }*/

    //提交
    @ApiOperation(value = "提交【前端不验证，可多次提交】", notes = "提交【前端不验证，可多次提交】")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @RequestMapping(value = REQUEST_MODEL + "/commitNew", method = RequestMethod.GET)
    public Result<?> deliveryCommitNew(
            String gdnNo,BigDecimal gdnHeaderId,
            HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws Exception {
        try {
            return deliveryNewService.deliveryCommit(gdnNo,gdnHeaderId,servletRequest,servletResponse);
        }
        catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    //清除缓存
    @ApiOperation(value = "/【测试，前端别调】清除缓存", notes = "/【测试，前端别调】清除缓存")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @RequestMapping(value = REQUEST_MODEL + "/deleteCache", method = RequestMethod.GET)
    public void deleteCache(
            String gdnNo,
            HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
        try {
             deliveryNewService.deleteCache(gdnNo);
        } catch (Exception ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            ex.printStackTrace();
            Shift.fatal("库存不足");
        }
    }


}
